import cv2
import matplotlib.pyplot as plt
import numpy as np


def cv2_show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == "__main__":
    # Canny edge detection
    img_gray = cv2.imread("../images/test_write.jpg", cv2.IMREAD_GRAYSCALE)
    img = cv2.imread("../images/Camera_A_1.jpg", cv2.IMREAD_GRAYSCALE)
    hist_pic = cv2.calcHist([img], [0], None, [256], [0, 256])
    print(img.shape)
    # plt.plot(hist_pic)
    # plt.show()
    mask = np.zeros(img.shape[:2], np.uint8)
    mask[100:500, 100:1000] = 255
    masked_img = cv2.bitwise_and(img, img, mask=mask)
    # cv2_show("result", masked_img)
    hist_full = cv2.calcHist([img], [0], None, [256], [0, 255])
    hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 255])
    # plt.subplot(221), plt.imshow(img, "gray")
    # plt.subplot(222), plt.imshow(mask, "gray")
    # plt.subplot(223), plt.imshow(masked_img, "gray")
    # plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
    # plt.show()
    equ_img = cv2.equalizeHist(img)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    clahe_img = clahe.apply(img)
    res = np.hstack((img, equ_img, clahe_img))
    cv2_show("result", res)
